package com.ideal.util;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 文件操作工具类
 *
 * @author ideal-20
 * @date 2022-10-30 14:21
 **/
@Component
@Slf4j
public class FileUtil {
    /**
     * <h2> 遍历目录中下的所有文件 </h2>
     *
     * @param localPath    路径
     * @param dirRecursive 是否递归遍历子目录
     * @param fileList     调用方需要自己传入结果 list 承接数据
     */
    public static void readFileInDir(String localPath, boolean dirRecursive, List<String> fileList) {
        File localFile = new File(localPath);
        File[] localFiles = localFile.listFiles();
        if (localFiles == null || localFiles.length == 0) {
            return;
        }

        Arrays.sort(localFiles, (o1, o2) -> {
            if (o1.isDirectory() && o2.isFile()) {
                return -1;
            }
            if (o1.isFile() && o2.isDirectory()) {
                return 1;
            }
            return o1.getName().compareTo(o2.getName());
        });

        for (File file : localFiles) {
            if (file.isDirectory() && dirRecursive) {
                readFileInDir(file.getAbsolutePath(), Boolean.TRUE, fileList);
            } else {
                if (isMarkDownFile(file.getName())) {
                    fileList.add(file.getAbsolutePath());
                }
            }
        }
        log.info("readFileInDir filePathList:{}", fileList);
    }

    /**
     * <h2> 判断文件是否 markdown </h2>
     *
     * @param fileName 文件名
     */
    public static boolean isMarkDownFile(String fileName) {
        boolean result = false;
        String suffix = fileName.substring(fileName.lastIndexOf('.') + 1);
        if ("md".equals(suffix)) {
            result = true;
        }
        return result;
    }

    /**
     * <h2> 获取新文件目录</h2>
     */
    public static String getTargetDirPathBySource(String sourceFilePath, String sourceRootDirPath, String targetRootDirPath) {
        // 直接将文件的目录换成新目录
        String targetFilePath = sourceFilePath.replaceAll(sourceRootDirPath, targetRootDirPath);
        int splitIndex = getFileSplitIndex(targetFilePath);
        return targetFilePath.substring(0, splitIndex + 1);
    }

    /**
     * <h2> 获取新文名</h2>
     */
    public static String getTargetFileNamePathBySource(String sourceFilePath, String sourceRootDirPath, String targetRootDirPath) {
        // 直接将文件的目录换成新目录
        String targetFilePath = sourceFilePath.replaceAll(sourceRootDirPath, targetRootDirPath);
        int splitIndex = getFileSplitIndex(targetFilePath);
        return targetFilePath.substring(splitIndex + 1);
    }

    private static int getFileSplitIndex(String targetFilePath) {
        // 不同系统分隔符不同，默认 mac，兼容 windows
        String splitStr = "/";
        if (SystemUtils.isWindows()) {
            splitStr = "\\";
        }
        return targetFilePath.lastIndexOf(splitStr);
    }

}